package org.fuxia.im.gui;

import java.awt.Font;
import java.awt.Image;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JLabel;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import java.awt.Insets;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import javax.swing.UIManager;

import org.fuxia.im.common.ClientContext;
import org.fuxia.im.entities.User;

import com.alibaba.fastjson.JSONArray;

import java.awt.Color;

public class AddFriendNext extends JFrame {

	private static final long serialVersionUID = 1L;
	private JPanel contentPane;
//	private User user ;

	/**
	 * Create the frame.
	 */
	public AddFriendNext(final int width, final int height,final int x,final int y, String result, int flog, final String[] user) {
		setResizable(false);
		setTitle(ClientContext.getLoginUser().getNickName()+" - 添加好友");
		setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
		setBounds(x+(width-460)/2, y+(height-340)/2, 460, 340);

		
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		contentPane.setLayout(null);
		ImageIcon icon = new ImageIcon(Thread.currentThread().getContextClassLoader().getResource("images/lifangfang.jpg")) ;
		icon.setImage(icon.getImage().getScaledInstance(108, 108, Image.SCALE_DEFAULT));
		
		JPanel panel = new JPanel();
		panel.setBackground(Color.WHITE);
		panel.setBounds(120, 0, 334, 276);
		contentPane.add(panel);
		panel.setLayout(null);
		

		JLabel successTip = new JLabel(result);
		if(flog==0) {
			successTip.setIcon(new ImageIcon(Thread.currentThread().getContextClassLoader().getResource("images/tip.jpg")));
		}else{
			successTip.setIcon(new ImageIcon(Thread.currentThread().getContextClassLoader().getResource("images/addSuccess.jpg")));
		}

		successTip.setFont(new Font("微软雅黑", Font.PLAIN, 13)) ;
		successTip.setBounds(10, 22, 300, 30);
		panel.add(successTip);
		
		JPanel panel_1 = new JPanel();
		panel_1.setBackground(Color.WHITE);
		panel_1.setBorder(UIManager.getBorder("FormattedTextField.border"));
		panel_1.setBounds(0, 276, 454, 31);
		contentPane.add(panel_1);
		panel_1.setLayout(null);
		
		JButton next = new JButton("完成");
		next.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				dispose() ;
			}
		});
		next.setFont(new Font("微软雅黑", Font.PLAIN, 12)) ;
		next.setMargin(new Insets(2, 2, 2, 2));
		next.setBounds(324, 4, 62, 23);
		panel_1.add(next);
		
		JButton cancel = new JButton("发起会话");
		cancel.setFont(new Font("微软雅黑", Font.PLAIN, 12)) ;
		cancel.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				Object obj = ClientContext.getValue(user[2]);
				Chat chat = null ;
				if(obj!=null && obj instanceof Chat){
					chat = (Chat)obj;
					chat.setState(JFrame.NORMAL);
				}else {
					User temp = new User() ;
					temp.setImagePath(user[0]) ;
					temp.setNickName(user[1]) ;
					temp.setUserCode(user[2]) ;
					chat = new Chat(temp) ;
					ClientContext.getContext().put(user[2], chat);
					chat.setVisible(true);
				}
			}
		});
		cancel.setMargin(new Insets(0, 0, 0, 0));
		cancel.setBounds(389, 4, 62, 23);
		panel_1.add(cancel);
		
		JPanel panel_2 = new JPanel();
		panel_2.setBackground(Color.WHITE);
		panel_2.setBorder(UIManager.getBorder("FormattedTextField.border"));
		panel_2.setBounds(0, 0, 119, 276);
		contentPane.add(panel_2);
		panel_2.setLayout(null);
		
		JLabel head = new JLabel("");
		head.setIcon(new ImageIcon(Thread.currentThread().getContextClassLoader().getResource(user[0])));
		head.setBounds(10, 0, 108, 108);
		panel_2.add(head);
		
		JLabel nickname = new JLabel(user[1]);
		nickname.setFont(new Font("微软雅黑", Font.BOLD, 12)) ;
		nickname.setBounds(10, 120, 84, 15);
		panel_2.add(nickname);
		
		JLabel userCode = new JLabel(user[2]);
		userCode.setFont(new Font("微软雅黑", Font.BOLD, 12)) ;
		userCode.setBounds(10, 150, 84, 15);
		panel_2.add(userCode);
		
		JLabel sex = new JLabel("性别："+user[3]);
		sex.setFont(new Font("微软雅黑", Font.PLAIN, 12)) ;
		sex.setBounds(10, 180, 84, 15);
		panel_2.add(sex);
		
		JLabel age = new JLabel("年龄："+user[4]);
		age.setFont(new Font("微软雅黑", Font.PLAIN, 12)) ;
		age.setBounds(10, 210, 84, 15);
		panel_2.add(age);
	}
	
	public JFrame getFrame() {
		return this ;
	}
}
